﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS;
using PMS.Resources;
using PMS.Win;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;

namespace PMS.ChucNang
{
    public partial class ChiTietXuatLookup : FormPopupBase
    {
        private readonly ChiTietPhieuXuatBO service = new ChiTietPhieuXuatBO();
        public ChiTietPhieuXuat ChiTietPhieuXuatSelected;
        string MaThuoc = null;
        bool IsThanhToan = false;
        public ChiTietXuatLookup(string mathuoc = null)
        {
            MaThuoc = mathuoc;
            InitializeComponent();
            InitStyle();
            InitData();
        }

        public ChiTietXuatLookup( bool isForThanhToan, string soPX = null)
        {
            MaThuoc = soPX;
            IsThanhToan = isForThanhToan;
            InitializeComponent();
            InitStyle();
            InitData();
        }
      
        void InitStyle()
        {
            grvChiTietPhieuXuatList.OptionsBehavior.Editable = false;
            grvChiTietPhieuXuatList.OptionsView.ShowGroupPanel = false;
            grvChiTietPhieuXuatList.OptionsView.ShowIndicator = true;

            grvChiTietPhieuXuatList.OptionsView.ShowAutoFilterRow = true;
            grvChiTietPhieuXuatList.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
            grvChiTietPhieuXuatList.OptionsView.ShowGroupedColumns = false;
            grvChiTietPhieuXuatList.OptionsMenu.EnableColumnMenu = false;
            grvChiTietPhieuXuatList.OptionsCustomization.AllowColumnMoving = false;
            grvChiTietPhieuXuatList.OptionsCustomization.AllowGroup = false;
            grvChiTietPhieuXuatList.OptionsFilter.AllowFilterEditor = false;

            this.grvChiTietPhieuXuatList.OptionsSelection.MultiSelect = false;
            this.grvChiTietPhieuXuatList.IndicatorWidth = Constants.INDICATOR_WIDTH;
            //gridbinding
            this.colCTPX.FieldName = ChiTietPhieuXuatColumns.MaCTPX.ToString();
            this.colMaThuoc.FieldName = LuuKhoColumns.MaThuoc.ToString();
            this.colSoLo.FieldName = LuuKhoColumns.SoLo.ToString();
            this.colDonGia.FieldName = ChiTietPhieuXuatColumns.DonGia.ToString();
            this.colTonKho.FieldName = ChiTietPhieuXuatColumns.SoLuong.ToString();
            this.colNgaySX.FieldName = LuuKhoColumns.NgaySX.ToString();
            this.colHanDung.FieldName = LuuKhoColumns.HanDung.ToString();
            this.colHanSD.FieldName = LuuKhoColumns.HanSuDung.ToString();
            //this.colThanhTien.FieldName = ChiTietPhieuXuatColumns.GhiChu.ToString();
            

        }

        void InitData()
        {
            try
            {
                if (MaThuoc == null)
                {
                    this.grdChiTietPhieuXuatList.DataSource = service.GetAllChiTietPhieuXuat();
                }
                else
                {
                    if(IsThanhToan)
                    {
                        this.grdChiTietPhieuXuatList.DataSource = service.GetAllChiTietPhieuXuatBySoPX(MaThuoc);
                    }else
                    {
                    this.grdChiTietPhieuXuatList.DataSource = service.GetAllChiTietPhieuXuat();
                    }
                }
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

        }

        private void CloseDialog(DialogResult result)
        {
            try
            {
                if (result == DialogResult.OK)
                {
                    this.ChiTietPhieuXuatSelected = (ChiTietPhieuXuat)grvChiTietPhieuXuatList.GetFocusedRow();
                }
                else
                {
                    this.ChiTietPhieuXuatSelected = null;
                }

                this.DialogResult = result;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.OK);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.Cancel);
        }

        private void grvChiTietPhieuXuatList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if ((e.Info.IsRowIndicator) && (e.RowHandle >= 0))
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void grvChiTietPhieuXuatList_DoubleClick(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hi = view.CalcHitInfo(grdChiTietPhieuXuatList.PointToClient(MousePosition));
            if (hi.InRow || hi.InRowCell)
            {
                if (ChiTietPhieuXuatSelected != null)
                {
                    CloseDialog(DialogResult.OK);
                }
            }
        }

        private void grvChiTietPhieuXuatList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                btnOk_Click(sender, null);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnCancel_Click(sender, null);
            }
        }

        private void grvChiTietPhieuXuatList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            ChiTietPhieuXuatSelected = this.grvChiTietPhieuXuatList.GetFocusedRow() as ChiTietPhieuXuat;
        }

        private void grvChiTietPhieuXuatList_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
